package example;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Genshin extends Frame {
    int xI = 1, xL = 1, xR = 1;
    boolean isFindI = false, isFindL = false, isFindR = false;
    long findImgTime = 0;
    boolean autoClick = false;

    private Robot robot = new Robot();
    private String mouseXY = "test mouse xy";

    public Genshin() throws AWTException, InterruptedException, IOException {
        setUndecorated(true);
        // this.setOpacity(0.5f);
        // this.setBackground(new Color(255, 255, 255, 0));
        this.setBounds(0, 0, 1920, 100);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_F10) {
                    autoClick = !autoClick;
                }
            }
        });
        this.setVisible(true);
        // int x = 737, y = 170;
        // int w = 443, h = 30;
        int x = 0, y = 0, w = 1920, h = 1200;
        Rectangle rectangle = new Rectangle(x, y, w, h);
        while (true) {
            long startTime = System.currentTimeMillis();
            BufferedImage screenCapture = robot.createScreenCapture(rectangle);
            // ImageIO.write(screenCapture, "jpg", new File("C:\\Users\\Johnson\\Desktop\\screen.jpg"));
            // 开始找图
            // rgb(255 255 192) = -64
            // rgb(255 255 255) = -1
            boolean isFindAll = false;
            isFindI = false;
            isFindL = false;
            isFindR = false;
            for (int startY = 1; startY < h; startY++) {
                for (int startX = 1; startX < w; startX++) {
                    int rgb = screenCapture.getRGB(startX, startY);
                    if (rgb == -64) {
                        if (!isFindI && checkI(screenCapture, startX, startY)) {
                            xI = startX + x;
                            isFindI = true;
                        }
                        if (!isFindL && checkL(screenCapture, startX, startY)) {
                            xL = startX + x;
                            isFindL = true;
                        }
                        if (!isFindR && checkR(screenCapture, startX, startY)) {
                            xR = startX + x;
                            isFindR = true;
                        }
                        isFindAll = isFindI && isFindL && isFindR;
                    }
                    if (isFindAll) break;
                }
                if (isFindAll) break;
            }
            findImgTime = System.currentTimeMillis() - startTime;
            // int rgb = screenCapture.getRGB(292, 292);
            // System.out.println(rgb);
            // System.out.println(convertArgbToRgb(xI));
            // Thread.sleep(100);
            repaint();
        }
    }

    // I = 7 * 26
    public boolean checkI(BufferedImage bi, int findX, int findY) {
        try {
            return bi.getRGB(findX + 6, findY) == -64 &&
                    bi.getRGB(findX, findY + 25) == -64 &&
                    bi.getRGB(findX + 6, findY + 25) == -64;
        } catch (Exception e) {
            return false;
        }
    }

    // < = 11 * 18
    public boolean checkL(BufferedImage bi, int findX, int findY) {
        try {
            return bi.getRGB(findX - 10, findY + 8) == -64 &&
                    bi.getRGB(findX, findY + 17) == -64;
        } catch (Exception e) {
            return false;
        }
    }

    // > = 11 * 18
    public boolean checkR(BufferedImage bi, int findX, int findY) {
        try {
            return bi.getRGB(findX + 8, findY + 7) == -64 &&
                    bi.getRGB(findX, findY + 14) == -64;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //将保存下的线重写绘制出来
        if (isFindI) drawLine(xI, g, 20);
        if (isFindL) drawLine(xL, g, 40);
        if (isFindR) drawLine(xR, g, 60);
        if (isFindI && isFindL && isFindR) {
            if (xI < xL + (xR - xL) / 2 && autoClick) {
                robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                System.out.println("点击");
            }
        }
        g.drawString("时间:" + findImgTime, 20, 20);
        g.drawString(mouseXY, 20, 40);
        g.drawString("自动点击:" + autoClick, 20, 60);
    }

    public void drawLine(int x, Graphics g, int h) {
        g.drawLine(x, 0, x, h);
    }

    public static void main(String[] args) throws Exception {
        Genshin genshin = new Genshin();
    }
}